package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyXinXiaoBai;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.Map;

/**
 * 安心分期-鑫小白-上海 苏州
 *
 * @author WJB
 */
@ApiPlan(productId = "342", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5XinXiaoBai implements ApiOrgPhoneMd5ConvertHandler {

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataChangeMessage) throws Exception {
        Map<Object, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("mobile_md5", apiDataChangeMessage.getPhoneMd5());
        });
        return JsonUtils.toJson(map);
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        boolean ResultCode = Eq.object(jsonObject.getInteger("ResultCode"), 0);
        if (!ResultCode) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }

        boolean code = Eq.object(jsonObject.getInteger("Code"), 200);
        if (!code) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter ApiApplyMessage) throws Exception {
        //  拓展判断信息
        com.ts.api.module.api.convert.core.utils.ApiFieldConverter apiFieldConverter = com.ts.api.module.api.convert.core.utils.ApiFieldConverter.create(ApiApplyMessage);

        OrgApplyXinXiaoBai apply = new OrgApplyXinXiaoBai();
        apply.setName(ApiApplyMessage.getUserName());
        //  性别
        Integer sex = ChooseEq.create()
                .when(ApiApplyMessage.getSex(), "0", 1)
                .when(ApiApplyMessage.getSex(), "1", 0)
                .end(1);
        apply.setSex(sex);
        //  年龄
        apply.setAge(apiFieldConverter.getAgeInt());
        //  贷款额度
        String money = ChooseEq.create()
                .when(ApiApplyMessage.getLoanAmount(), "1", "3万")
                .when(ApiApplyMessage.getLoanAmount(), "2", "5万")
                .when(ApiApplyMessage.getLoanAmount(), "3", "10万")
                .when(ApiApplyMessage.getLoanAmount(), "4", "20万")
                .end("3万");
        apply.setQuota(money);
        apply.setPhoneNumber(ApiApplyMessage.getPhone());
        //  房产
        if (ValidParam.isNotEmpty(ApiApplyMessage.getRealEstateInfo())) {
            Boolean house = ChooseEq.create()
                    .when(ApiApplyMessage.getRealEstateInfo(), "1", false)
                    .when(ApiApplyMessage.getRealEstateInfo(), "2", true)
                    .when(ApiApplyMessage.getRealEstateInfo(), "3", true)
                    .when(ApiApplyMessage.getRealEstateInfo(), "4", true)
                    .when(ApiApplyMessage.getRealEstateInfo(), "5", true)
                    .when(ApiApplyMessage.getRealEstateInfo(), "6", true)
                    .end();
            apply.setHouse(house);
        }
        //  车产
        if (ValidParam.isNotEmpty(ApiApplyMessage.getCarInfo())) {
            Boolean car = ChooseEq.create()
                    .when(ApiApplyMessage.getCarInfo(), "1", false)
                    .when(ApiApplyMessage.getCarInfo(), "2", true)
                    .when(ApiApplyMessage.getCarInfo(), "3", true)
                    .when(ApiApplyMessage.getCarInfo(), "4", true)
                    .end();
            apply.setCar(car);
        }
        //  公积金
        if (ValidParam.isNotEmpty(ApiApplyMessage.getProvidentFund())) {
            Boolean text = ChooseEq.create()
                    .when(ApiApplyMessage.getProvidentFund(), "1", false)
                    .when(ApiApplyMessage.getProvidentFund(), "2", true)
                    .when(ApiApplyMessage.getProvidentFund(), "3", true)
                    .when(ApiApplyMessage.getProvidentFund(), "4", true)
                    .end();
            apply.setPFG(text);
        }
        //  保单
        if (ValidParam.isNotEmpty(ApiApplyMessage.getInsurancePolicy())) {
            Boolean text = ChooseEq.create()
                    .when(ApiApplyMessage.getInsurancePolicy(), "1", false)
                    .when(ApiApplyMessage.getInsurancePolicy(), "2", true)
                    .when(ApiApplyMessage.getInsurancePolicy(), "3", true)
                    .when(ApiApplyMessage.getInsurancePolicy(), "4", true)
                    .end();
            apply.setWarranty(text);
        }
        //   月收入
        String monthlySalary = ChooseEq.create()
                .when(ApiApplyMessage.getMonthlySalary(), "1", "3000")
                .when(ApiApplyMessage.getMonthlySalary(), "2", "8000")
                .when(ApiApplyMessage.getMonthlySalary(), "3", "15000")
                .when(ApiApplyMessage.getMonthlySalary(), "4", "30000")
                .when(ApiApplyMessage.getMonthlySalary(), "5", "50000")
                .end();
        apply.setIncome(monthlySalary);
        apply.setDataCompany(39);
        String city = ApiApplyMessage.getCity();
        if (city.contains("上海")) {
            apply.setAscription(0);
        } else if (city.contains("无锡")) {
            apply.setAscription(1);
        } else if (city.contains("苏州")) {
            apply.setAscription(2);
        }

        return JsonUtils.toJson(apply);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        boolean ResultCode = Eq.object(jsonObject.getInteger("ResultCode"), 0);
        if (!ResultCode) {
            return RespOrgPhoneMd5Apply.ofFail();
        }

        boolean Code = Eq.object(jsonObject.getInteger("Code"), 200);
        if (!Code) {
            return RespOrgPhoneMd5Apply.ofFail();
        }

        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
